package com.jiamao.goplay.ui.discover.vm;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.bean.BaseRep;
import com.jiamao.goplay.bean.SearchAddrs;
import com.jiamao.goplay.bean.res.ResSearchAddr;
import com.jiamao.goplay.bean.res.ResUserInfoDetail;
import com.jiamao.goplay.data.BaseSubscriber;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.ui.user.vm.EditUserInfoNM;
import com.jiamao.goplay.util.GsonUtil;
import com.jiamao.goplay.util.KeyInputUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class SearchVM extends BaseViewModel<DataRepository> {

    private int limt = 20;
    private int page =1 ;

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");

    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt(View.INVISIBLE);


    public SearchVM(@NonNull Application application, DataRepository repository) {
        super(application, repository);

//        //模拟10个条目，数据源可以来自网络
//        for (int i = 0; i < 20; i++) {
//            SearchItemVM item = new SearchItemVM(this);
//            observableList.add(item);
//
//        }

    }


    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Integer> showProg = new SingleLiveEvent<>();


    }


    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                clearBtnVisibility.set(View.VISIBLE);
            } else {
                clearBtnVisibility.set(View.INVISIBLE);
            }
        }
    });

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand backOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand searchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //finish();
//            KeyInputUtils.hideSoftInp;
            searchAddr(userName.get(),0);
        }
    });

    //给RecyclerView添加ObservableList
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            String itemType = (String) item.getItemType();

            itemBinding.set(BR.viewModel, R.layout.discover_search_item_layout);

        }
    });


    public void searchAddr(String key,int dir){
        if (dir== 0 ){
            page = 0;
        }

        BaseSubscriber subscription = new BaseSubscriber<BaseRep<ResSearchAddr>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                uc.showProg.setValue(1);
            }

            @Override
            public void onComplete() {

            }

            @Override
            public void onNext(BaseRep<ResSearchAddr> baseRep) {
                super.onNext(baseRep);
                uc.showProg.setValue(1);
                //保存账号密码
                if (baseRep.isOk()){

                    if (dir == 0){
                        observableList.clear();
                    }

                    for (SearchAddrs addrs:baseRep.getData().getList()){

                        SearchItemVM item = new SearchItemVM(SearchVM.this,addrs);
                        observableList.add(item);
                    }

                }else {
                    //    RxToast.showToastShort(baseRep.getMsg());
//
                }
            }
        };
        page++;
        model.getApi().searchAddr(userName.get(),limt,page,"")
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())  // 线程调度
                .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
                //    .doOnSubscribe(this) //请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(subscription);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
